<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info"
                  show-icon/>

        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader
            :buttons="['refresh', 'comSearch']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('agent.withdrawal.quick Search Fields') })"
        >
            <template #default>
                <el-button v-blur v-if="auth('add') && !adminInfo.is_show_city"
                           @click="add" class="table-header-operate"
                           type="primary">
                    <Icon name="el-icon-Promotion"/>
                    <span class="table-header-operate-text">提现</span>
                </el-button>
            </template>

        </TableHeader>
        <h4 v-if="auth('add') && !adminInfo.is_show_city">余额：<span style="color: red;">{{ adminInfo.money }}元</span>
        </h4>
        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        <Table ref="tableRef"></Table>

        <!-- 表单 -->
        <PopupForm/>
    </div>
</template>

<script setup lang="ts">
import {markRaw, onMounted, provide, ref} from 'vue'
import {useI18n} from 'vue-i18n'
import PopupForm from './popupForm.vue'
import {baTableApi} from '/@/api/common'
import {defaultOptButtons} from '/@/components/table'
import TableHeader from '/@/components/table/header/index.vue'
import Table from '/@/components/table/index.vue'
import baTableClass from '/@/utils/baTable'
import IdFieldRender from "./idFieldRender.vue";
import {payment, reject} from "/@/api/backend/agent";
import {ElMessage, ElMessageBox} from "element-plus";
import {auth} from "/@/utils/common";
import {useAdminInfo} from "/@/stores/adminInfo";
import {index} from "/@/api/backend";

defineOptions({
    name: 'agent/withdrawal',
})

const {t} = useI18n()
const tableRef = ref()
const optButtons: OptButton[] = defaultOptButtons([])


// 自定义一个新的按钮
let newButton: OptButton[] = [
    {
        // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
        render: 'confirmButton',
        // 按钮名称
        name: 'info',
        // 鼠标放置时的 title 提示
        title: '打款',
        // 直接在按钮内显示的文字，title 有值时可为空
        text: '打款',
        // 按钮类型，请参考 element plus 的按钮类型
        type: 'success',
        // 按钮 icon
        icon: 'el-icon-CircleCheckFilled',
        class: 'table-row-info',
        // tipButton 禁用 tip
        disabledTip: false,
        popconfirm: {
            title: '确认打款吗？'
        },
        // 自定义点击事件
        click: (row: TableRow, field: TableColumn) => {
            payment({id: row.withdrawal_id}).then(res => {
                if (res.code != 1) {
                    ElMessage({
                        type: 'error',
                        message: res.msg,
                    })
                    return
                }
                ElMessage({
                    type: 'success',
                    message: '打款成功',
                })
                baTable.getIndex()?.then(() => {
                    baTable.initSort()
                    baTable.dragSort()
                })
            })
        },
        // 按钮是否显示，请返回布尔值
        display: (row: TableRow, field: TableColumn) => {
            if (!auth('payment')) return false;
            if (row.status != 0) return false;
            return true
        },

    },
    {
        // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
        render: 'tipButton',
        // 按钮名称
        name: 'info',
        // 鼠标放置时的 title 提示
        title: '驳回',
        // 直接在按钮内显示的文字，title 有值时可为空
        text: '驳回',
        // 按钮类型，请参考 element plus 的按钮类型
        type: 'danger',
        // 按钮 icon
        icon: 'el-icon-CircleCloseFilled',
        class: 'table-row-info',
        // tipButton 禁用 tip
        disabledTip: false,
        // 自定义点击事件
        click: (row: TableRow, field: TableColumn) => {
            ElMessageBox.prompt('请填写驳回原因：', '驳回原因', {
                confirmButtonText: '驳回',
                cancelButtonText: '取消',
                inputValidator: (value) => {
                    if (!value) {
                        return '请填写驳回原因'
                    }
                },
                inputErrorMessage: '请填写驳回原因',
            })
                .then(({value}) => {
                    reject({id: row.withdrawal_id, msg: value}).then(res => {
                        if (res.code != 1) {
                            ElMessage({
                                type: 'error',
                                message: res.msg,
                            })
                            return
                        }
                        ElMessage({
                            type: 'success',
                            message: `驳回成功，驳回原因：${value}`,
                        })
                        baTable.getIndex()?.then(() => {
                            baTable.initSort()
                            baTable.dragSort()
                        })
                    })
                })
                .catch(() => {
                    ElMessage({
                        type: 'info',
                        message: 'Input canceled',
                    })
                })
        },
        // 按钮是否显示，请返回布尔值
        display: (row: TableRow, field: TableColumn) => {
            if (!auth('reject')) return false;
            if (row.status != 0) return false;
            return true
        },

        // 自定义el-button属性
        attr: {}
    },
]
// 新按钮合入到默认的按钮数组
let optBtn = newButton.concat(optButtons)
const adminInfo = useAdminInfo();


/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/admin/agent.Withdrawal/'),
    {
        pk: 'withdrawal_id',
        column: [
            // {type: 'selection', align: 'center', operator: false},
            {
                label: t('agent.withdrawal.withdrawal_id'),
                prop: 'withdrawal_id',
                align: 'center',
                width: 70,
                operator: 'RANGE',
                sortable: 'custom'
            },
            {
                label: "名称",
                prop: 'agent.nickname',
                align: 'center',
                operator: 'LIKE',
                sortable: false,
                show: adminInfo.is_show_city
            },
            {
                label: "提现信息",
                prop: 'card_number',
                align: 'center',
                render: 'customRender',
                customRender: markRaw(IdFieldRender),
                operator: false,
            },
            {label: t('agent.withdrawal.price'), prop: 'price', align: 'center', operator: false, sortable: false},

            {
                label: t('agent.withdrawal.status'),
                prop: 'status',
                align: 'center',
                operator: 'eq',
                sortable: false,
                render: 'tag',
                replaceValue: {
                    '0': '申请中',
                    '1': '已打款',
                    '2': '已驳回'
                },
                custom: {
                    '1': 'success',
                    '2': 'danger'
                }
            },
            {
                label: "备注",
                prop: 'msg',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: false,
                sortable: false
            },
            {
                label: t('agent.withdrawal.create_time'),
                prop: 'create_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss'
            },
            {
                label: t('Operate'),
                align: 'center',
                width: 200,
                render: 'buttons',
                buttons: optBtn,
                operator: false,
                show: adminInfo.is_show_city
            },
        ],
        dblClickNotEditColumn: ['all'],
        showComSearch: true,
        defaultOrder: {prop: 'withdrawal_id', order: 'desc'},
    },
    {
        defaultItems: {price: 0, status: 0},
    }
)
const add = ()=>{
    baTable.onTableHeaderAction('add')
}
provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
</script>

<style scoped lang="scss"></style>
